package stock.nordnet;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import misc.Helpers;
import misc.JsonEncodable;
import net.sourceforge.javajson.JsonObject;

import com.gargoylesoftware.htmlunit.html.*;

/**
 * @author Filip
 */
public class NordNetDirektDepoParser {

    public List<JsonEncodable> getDepo(final HtmlPage page) {
        String path = "//div[@id='main']/table/tbody";
        List<JsonEncodable> entries = new ArrayList<>();
        boolean ownsAnyStock = true;
        HtmlTableBody tbody = null;
        try {
            tbody = (HtmlTableBody) page.getByXPath(path).get(0);
        } catch (Exception e) {
            ownsAnyStock = false;
        }
        if (ownsAnyStock) {
            List<HtmlTableRow> rows = tbody.getRows();

            for (HtmlTableRow row : rows) {
                if (row.hasAttribute("class")
                        && "odd even".indexOf(row.getAttribute("class")) != -1) {
                    DepoEntry e = new DepoEntry();
                    e.stock = Helpers.formatName(row.getCell(0).asText());
                    e.latestPay = row.getCell(3).asText();
                    e.day = row.getCell(4).asText();
                    e.bought = row.getCell(7).asText();
                    e.current = row.getCell(8).asText();
                    e.totalRate = row.getCell(9).asText();
                    e.total = row.getCell(10).asText();
                    e.dayRate = parse(e.day)
                            / (parse(e.latestPay) - parse(e.day)) * 100 + "";
                    entries.add(e);
                }
            }
        } else {
            DepoEntry e = new DepoEntry();
            e.stock = "No stocks are owned at this time";
            entries.add(e);
        }
        return entries;
    }

    public static double parse(String s) {
        return Double.parseDouble(s.replace(',', '.').replace(" ", ""));
    }

    public String fetchLatestValue(final HtmlPage page) {
        String accSumString = ((HtmlTableDataCell) page
                .getByXPath(
                        "/html/body/div[@id='container']"
                                + "/div[@class='content contentline']/div[@id='main']"
                                + "/div[@class='twocolleft']/table[@class='tabelldata']"
                                + "/tbody/tr[@class='tablesum']/td[2]").get(0))
                .asText();
        accSumString = accSumString.replace(" ", "");
        accSumString = accSumString.replace(',', '.');
        return accSumString;
    }

    public String fetchLatestRate(final HtmlPage page) {
        String accSumString = ((HtmlSpan) page.getByXPath(
                "//div[@id='main']/div[2]/div/span").get(0)).asText();
        return accSumString;
    }

    private static class DepoEntry implements JsonEncodable {
        private static DecimalFormat df = new DecimalFormat("#0.00");
        private String stock = "";
        private String latestPay = "";
        private String day = "";
        private String dayRate = "";
        private String total = "";
        private String totalRate = "";
        private String bought = "";
        private String current = "";

        private final String _SEP_ = " - ";

        @Override
        public String toString() {
            return stock + _SEP_ + latestPay + _SEP_ + day + _SEP_ + dayRate
                    + _SEP_ + total + _SEP_ + totalRate + _SEP_ + bought
                    + _SEP_ + current;
        }

        @Override
        public void putJSON(final JsonObject jo) {
            jo.put("stock", stock);
            jo.put("latestPay", latestPay);
            jo.put("day", day);
            jo.put("dayRate", df.format(Double.parseDouble(dayRate)));
            jo.put("total", total);
            jo.put("totalRate", totalRate);
            jo.put("bought", bought);
            jo.put("current", current);
        }
    }

}
